'''
Descripttion: 
version: 
Author: Zhang Lingxin
Date: 2023-05-30 08:32:37
LastEditors: Zhang Lingxin
LastEditTime: 2023-05-30 23:07:50
'''
from rest_framework import serializers

from exam.models import Paper, Practice
from exam.serializers import PaperSerializer, PracticeSerializer
from question.models import Choice, Fill, Judge, Program, Text
from question.serializers import ChoiceSerializer, FillSerializer, JudgeSerializer, ProgramSerializer, TextSerializer
from record.models import *
from user.models import Student
from user.serializers import StudentSerializer


class ChoiceRecordSerializer(serializers.ModelSerializer):
    # 覆盖外键字段 只读
    practice = PracticeSerializer(read_only=True)
    student = StudentSerializer(read_only=True)
    choice = ChoiceSerializer(read_only=True)

    # 用于创建的只写字段
    # 反序列化时，必要的，分别从choice student practice模型实例化对象中找字段
    practice_id = serializers.PrimaryKeyRelatedField(queryset=Practice.objects.all(), source='practice',
                                                     write_only=True)
    student_id = serializers.PrimaryKeyRelatedField(queryset=Student.objects.all(), source='student', write_only=True)
    choice_id = serializers.PrimaryKeyRelatedField(queryset=Choice.objects.all(), source='choice', write_only=True)

    class Meta:
        model = ChoiceRecord
        fields = '__all__'


class FillRecordSerializer(serializers.ModelSerializer):
    # 覆盖外键字段 只读
    practice = PracticeSerializer(read_only=True)
    student = StudentSerializer(read_only=True)
    fill = FillSerializer(read_only=True)

    # 用于创建的只写字段
    practice_id = serializers.PrimaryKeyRelatedField(queryset=Practice.objects.all(), source='practice',
                                                     write_only=True)
    student_id = serializers.PrimaryKeyRelatedField(queryset=Student.objects.all(), source='student', write_only=True)
    fill_id = serializers.PrimaryKeyRelatedField(queryset=Fill.objects.all(), source='fill', write_only=True)

    class Meta:
        model = FillRecord
        fields = '__all__'


class JudgeRecordSerializer(serializers.ModelSerializer):
    # 覆盖外键字段 只读
    practice = PracticeSerializer(read_only=True)
    student = StudentSerializer(read_only=True)
    judge = JudgeSerializer(read_only=True)

    # 用于创建的只写字段
    practice_id = serializers.PrimaryKeyRelatedField(queryset=Practice.objects.all(), source='practice',
                                                     write_only=True)
    student_id = serializers.PrimaryKeyRelatedField(queryset=Student.objects.all(), source='student', write_only=True)
    judge_id = serializers.PrimaryKeyRelatedField(queryset=Judge.objects.all(), source='judge', write_only=True)

    class Meta:
        model = JudgeRecord
        fields = '__all__'


class ProgramRecordSerializer(serializers.ModelSerializer):
    # 覆盖外键字段 只读
    practice = PracticeSerializer(read_only=True)
    student = StudentSerializer(read_only=True)
    program = ProgramSerializer(read_only=True)

    # 用于创建的只写字段
    practice_id = serializers.PrimaryKeyRelatedField(queryset=Practice.objects.all(), source='practice',
                                                     write_only=True)
    student_id = serializers.PrimaryKeyRelatedField(queryset=Student.objects.all(), source='student', write_only=True)
    program_id = serializers.PrimaryKeyRelatedField(queryset=Program.objects.all(), source='program', write_only=True)

    class Meta:
        model = ProgramRecord
        fields = '__all__'

class TextRecordSerializer(serializers.ModelSerializer):
    # 覆盖外键字段 只读
    practice = PracticeSerializer(read_only=True)
    student = StudentSerializer(read_only=True)
    text = TextSerializer(read_only=True)

    # 用于创建的只写字段
    practice_id = serializers.PrimaryKeyRelatedField(queryset=Practice.objects.all(), source='practice',
                                                     write_only=True)
    student_id = serializers.PrimaryKeyRelatedField(queryset=Student.objects.all(), source='student', write_only=True)
    text_id = serializers.PrimaryKeyRelatedField(queryset=Text.objects.all(), source='text', write_only=True)

    class Meta:
        model = TextRecord
        fields = '__all__'


# ============================================= exam =====================================================================================


class ChoiceRecordExamSerializer(serializers.ModelSerializer):
    # 覆盖外键字段 只读
    paper = PaperSerializer(read_only=True)
    student = StudentSerializer(read_only=True)
    choice = ChoiceSerializer(read_only=True)

    # 用于创建的只写字段
    # 反序列化时，必要的，分别从choice student practice模型实例化对象中找字段
    paper_id = serializers.PrimaryKeyRelatedField(queryset=Paper.objects.all(), source='paper',
                                                     write_only=True)
    student_id = serializers.PrimaryKeyRelatedField(queryset=Student.objects.all(), source='student', write_only=True)
    choice_id = serializers.PrimaryKeyRelatedField(queryset=Choice.objects.all(), source='choice', write_only=True)

    class Meta:
        model = ChoiceRecordexam
        fields = '__all__'


class FillRecordExamSerializer(serializers.ModelSerializer):
    # 覆盖外键字段 只读
    paper = PaperSerializer(read_only=True)
    student = StudentSerializer(read_only=True)
    fill = FillSerializer(read_only=True)

    # 用于创建的只写字段
    paper_id = serializers.PrimaryKeyRelatedField(queryset=Paper.objects.all(), source='paper',
                                                     write_only=True)
    student_id = serializers.PrimaryKeyRelatedField(queryset=Student.objects.all(), source='student', write_only=True)
    fill_id = serializers.PrimaryKeyRelatedField(queryset=Fill.objects.all(), source='fill', write_only=True)

    class Meta:
        model = FillRecordexam
        fields = '__all__'


class JudgeRecordExamSerializer(serializers.ModelSerializer):
    # 覆盖外键字段 只读
    paper = PaperSerializer(read_only=True)
    student = StudentSerializer(read_only=True)
    judge = JudgeSerializer(read_only=True)

    # 用于创建的只写字段
    paper_id = serializers.PrimaryKeyRelatedField(queryset=Paper.objects.all(), source='paper',
                                                     write_only=True)
    student_id = serializers.PrimaryKeyRelatedField(queryset=Student.objects.all(), source='student', write_only=True)
    judge_id = serializers.PrimaryKeyRelatedField(queryset=Judge.objects.all(), source='judge', write_only=True)

    class Meta:
        model = JudgeRecordexam
        fields = '__all__'


class ProgramRecordExamSerializer(serializers.ModelSerializer):
    # 覆盖外键字段 只读
    paper = PaperSerializer(read_only=True)
    student = StudentSerializer(read_only=True)
    program = ProgramSerializer(read_only=True)

    # 用于创建的只写字段
    paper_id = serializers.PrimaryKeyRelatedField(queryset=Paper.objects.all(), source='paper',
                                                     write_only=True)
    student_id = serializers.PrimaryKeyRelatedField(queryset=Student.objects.all(), source='student', write_only=True)
    program_id = serializers.PrimaryKeyRelatedField(queryset=Program.objects.all(), source='program', write_only=True)

    class Meta:
        model = ProgramRecordexam
        fields = '__all__'


class TextRecordExamSerializer(serializers.ModelSerializer):
    # 覆盖外键字段 只读
    paper = PaperSerializer(read_only=True)
    student = StudentSerializer(read_only=True)
    text = TextSerializer(read_only=True)

    # 用于创建的只写字段
    paper_id = serializers.PrimaryKeyRelatedField(queryset=Paper.objects.all(), source='paper',
                                                     write_only=True)
    student_id = serializers.PrimaryKeyRelatedField(queryset=Student.objects.all(), source='student', write_only=True)
    text_id = serializers.PrimaryKeyRelatedField(queryset=Text.objects.all(), source='text', write_only=True)

    class Meta:
        model = TextRecordexam
        fields = '__all__'